package controllers
import (
	//"github.com/astaxie/beego"
	m"LiquidMeasurement/models"
"LiquidMeasurement/forms"
//	"go-lang-idea-plugin/testData/mockSdk-1.1.2/src/pkg/fmt"
	"fmt"
	"strconv"
"LiquidMeasurement/lib"
)

type RoomController struct {
	AdminController
}

func (this *RoomController) CreateRoom() {
	method := this.Ctx.Input.Method()
	UserID := this.Ctx.Input.Param(":UserID")
	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {
		admin := (this.GetSessionAdmin(UserID))
		if method == "POST" {       //判断请求方式
			var form = new(forms.RoomManageForm)
			Errors := make([]lib.ERROR, 0, 10)
			if err := this.ParseForm(form); err == nil {
				if _,err_msg :=form.CheckValid();len(err_msg) != 0{
					Errors = append(Errors,err_msg...)
				}else{
					room := m.Room{Id:form.Id, Floor:form.Floor, RoomNum:form.RoomNum}
					_, err := m.AddRoom(&room)
					if (err == nil) {
						this.Ctx.Redirect(302, "/admin/show_rooms_list/" + UserID)
						return
					}else{
						Errors = append(Errors,lib.ERROR{0,"数据库错误"})
					}

				}
			}
			this.Data["admin"] = &admin
			this.Data["UserID"] = UserID
			this.Data["Errors"] = Errors
			this.TplName = "add_room.html"
			return

		}else {
			this.Data["UserID"] = UserID
			this.Data["admin"] = &admin
			this.TplName = "add_room.html"
			return
		}
	}else{
		this.Ctx.Redirect(302, "/admin/login")

	}
}


func (this *RoomController) DeleteRoom() {
	UserID := this.Ctx.Input.Param(":UserID")

	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {
		method := this.Ctx.Input.Method()
		if method == "POST" {       //判断请求方式
			var form = new(forms.DepartmentManageForm)
			//this.ParseForm(&mLoginType)
			if err := this.ParseForm(form); err != nil {
				respone_json := forms.SiteResponeType{forms.SITE_PARA_ERROR, forms.INVALIDS, forms.INVALIDS}
				this.Data["json"] = &respone_json
				this.ServeJSON();
				return
			}
			//department := m.Department{Id:form.Id,Name:form.Name,Detail:form.Detail}
			_,err := m.DelRoomsByID(form.Id)
			fmt.Println("err:", err)
			respone_json := forms.SiteResponeType{forms.SITE_SUCCESS, forms.INVALIDS, forms.INVALIDS}
			this.Data["json"] = &respone_json
			this.ServeJSON();
			//this.TplName = "show_departments.html"
			return
		}else {
			respone_json := forms.SiteResponeType{forms.SITE_METHOD_ERROR, "数据提交方式错误", forms.INVALIDS}
			this.Data["json"] = &respone_json
			this.ServeJSON();
		}
	}else{
		this.Ctx.Redirect(302, "/admin/login")
	}
}


func (this *RoomController) ShowRooms() {

}


func (this *RoomController) UpdateRoom() {
	UserID := this.Ctx.Input.Param(":UserID")
	id := this.Ctx.Input.Param(":id")
	Id, _ := strconv.ParseUint(id, 10, 64)

	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {
		admin := (this.GetSessionAdmin(UserID))
		method := this.Ctx.Input.Method()
		if method == "POST" {
			var form = new(forms.RoomManageForm)
			Errors := make([]lib.ERROR,0,10)
			if err := this.ParseForm(form); err == nil {
				if _, err_msg := form.CheckValid(); len(err_msg) != 0 {
					Errors = append(Errors, err_msg...)
				}else {
					model := m.Room{Id:form.Id, Floor:form.Floor, RoomNum:form.RoomNum}
					_, err := m.UpdateRoom(&model)
					if (err == nil) {
						this.Ctx.Redirect(302, "/admin/show_beds_list/" + UserID)
						return
					}else {
						Errors = append(Errors, lib.ERROR{0, "数据库更新错误"})
					}
				}
			}
			model, _ := m.GetRoomById(Id)
			this.Data["room"] = &model
			this.Data["UserID"] = UserID
			this.Data["Errors"] = &Errors
			this.Data["admin"] = &admin
			this.TplName = "update_room.html"
		}else {
			model, err := m.GetRoomById(Id)
			if (err == nil) {
				this.Data["room"] = &model
				this.Data["UserID"] = UserID
				this.Data["admin"] = &admin
				this.TplName = "update_room.html"
				return
			}else {
				this.Abort("401")
				return
			}
		}
	}else{
		this.Ctx.Redirect(302, "/admin/login")

	}
}

func (this *RoomController) ShowRoomsList() {
	UserID := this.Ctx.Input.Param(":UserID")
	admin := (this.GetSessionAdmin(UserID))

	rooms,_ := m.GetRoomList()
	this.Data["rooms"] = &rooms
	this.Data["UserID"] = UserID
	this.Data["admin"] = &admin
	this.TplName = "show_rooms.html"
	return
}
